﻿using System;
using System.Collections.Generic;
using System.Web;

using Joel.Net;
using umbraco.blog4.interfaces;
using umbraco.BusinessLogic;
#if UMB45
using umbraco.presentation.nodeFactory;
#else
using umbraco.NodeFactory;
#endif

namespace umbraco.blog4.spam {
    public class AkismetSpamChecker: SpamChecker {
        #region .ctor
        public AkismetSpamChecker() { this.ProviderName = "Akismet"; }
        #endregion

        public override Boolean Check(int nodeid, string UserAgent, string UserIp, string Author, string AuthorEmail, string AuthorUrl, string Content) {
            Akismet api = Initialize(nodeid);
            if (api != null) {
                AkismetComment comment = new AkismetComment();
                comment.UserAgent = UserAgent;
                comment.UserIp = UserIp;
                comment.CommentType = "comment";
                comment.CommentAuthor = Author;
                comment.CommentAuthorEmail = AuthorEmail;
                comment.CommentAuthorUrl = AuthorUrl;
                comment.CommentContent = Content;
                
                return api.CommentCheck(comment);
            } else {
                return false;
            }
        }

        public override void MarkAsHam(int nodeid, string Author, string AuthorEmail, string AuthorUrl, string Content) {
            Akismet api = Initialize(nodeid);
            if (api != null) {
                AkismetComment comment = new AkismetComment();
                comment.CommentType = "comment";
                comment.CommentAuthor = Author;
                comment.CommentAuthorEmail = AuthorEmail;
                comment.CommentAuthorUrl = AuthorUrl;
                comment.CommentContent = Content;
                api.SubmitHam(comment);
            }
        }

        public override void MarkAsSpam(int nodeid, string Author, string AuthorEmail, string AuthorUrl, string Content) {
            Akismet api = Initialize(nodeid);
            if (api != null) {
                AkismetComment comment = new AkismetComment();
                comment.CommentType = "comment";
                comment.CommentAuthor = Author;
                comment.CommentAuthorEmail = AuthorEmail;
                comment.CommentAuthorUrl = AuthorUrl;
                comment.CommentContent = Content;
                api.SubmitSpam(comment);
            }
        }

        private Akismet Initialize(int nodeid) {
            Blog b = Blog.Current(nodeid);
            Node n = new Node(b.Id);

            if (n.GetProperty("akismetAPIKey") != null &
                !string.IsNullOrEmpty(n.GetProperty("akismetAPIKey").Value)) {
                Akismet api = new Joel.Net.Akismet(
                    n.GetProperty("akismetAPIKey").Value,
                    "http://" + HttpContext.Current.Request.ServerVariables["HTTP_HOST"] + n.Url,
                    "Blog4Umbraco3");
                if (!api.VerifyKey()) {
                    Log.Add(LogTypes.Error, -1, "Akismet Key could not be verified, please check if you have a valid Akismet API Key");
                    return null;
                } else {
                    return api;
                }
            } else {
                return null;
            }
        }
    }
}
